package com.example.pm;
/*========网络访问接口============================
 * 主体功能：发送一个命令CMD和一组数据content，返回一组数据result。
 * 发送数据有sendCmd（CMD)、sendContent(content)、sendCC(CMD,content)，返回数据getResult
 * 服务器使用request(CMD)、request(content)接收数据，使用result回写数据。
 * 
*/
import android.content.*;
import android.net.*;
import java.io.*;
import java.net.*;

class Net
{
	private String content;
	private String result="";
	private String CMD="";

/*
	Net(String CmdContent,String talkContent){
		this.content=talkContent;
		this.CMD=CmdContent;
	}
	*/
	
	public void sendCmd(String command){
		this.CMD=command;
		this.content="";
	}

  public void sendContent(String ecContent){
   this.CMD="";
   this.content=ecContent;
   }
	public void sendCC(String command,String ecContent){
		this.CMD=command;
		this.content=ecContent;
	}
  private String[] analyzeResult(){
   //String resultForCmd="";
   //String resultForContent="";
   String[]  res=new String[2];
   if(this.result.trim()!=""){
   res=this.result.trim().split("[|]"); 
      }
	  else{
		  res[0]="";
		  res[1]="";
	  }
	  return res;

  } 
  public String getResult(){
	 // String[] ss=this.result.toString().trim().split("|");
	 // return ss[2];
return result;
  }
  public String getCMDResult(){
	return this.analyzeResult()[0];
   
	}
  public String getContentResult(){
   return this.analyzeResult()[1];
   }


	/**
	 * 检测当的网络（WLAN、3G/2G）状态
	 * @param context Context
	 * @return true 表示网络可用
	 */
	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (info != null && info.isConnected()) 
			{
				// 当前网络是连接的
				if (info.getState() == NetworkInfo.State.CONNECTED) 
				{
					// 当前所连接的网络可用
					return true;
				}
			}
		}
		return false;
	}
	
        /*
		
		
		*/
        public void start(){
		String target="";
		URL url;
		target="http://a1639757.sn22372.gzonet.com/pmAPI.asp";
		//target="http://27.136.176.20/PM/pmAPI.asp";
		try
		{
			url = new URL(target);

			HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
			/*采用post方式传递数据*/
			urlConn.setRequestMethod("POST");
			urlConn.setDoInput(true);
			urlConn.setDoOutput(true);
			urlConn.setUseCaches(false);
			urlConn.setInstanceFollowRedirects(true);
			urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			DataOutputStream out=new DataOutputStream(urlConn.getOutputStream());
			String param="CMD="+URLEncoder.encode(this.CMD,"utf-8")+"&content="+URLEncoder.encode(this.content,"utf-8");
			out.writeBytes(param);
			out.flush();
			out.close();
			//响应成功
			if(urlConn.getResponseCode()==HttpURLConnection.HTTP_OK){
				InputStreamReader in=new InputStreamReader(urlConn.getInputStream());
				BufferedReader buffer=new BufferedReader(in);
				String inputLine=null;
				while((inputLine=buffer.readLine())!=null){
					this.result+=inputLine+"\n";
				}
				in.close();
			}
			urlConn.disconnect();
		}
		catch(MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

		}

		
	}